from src.sprite.Plane import *
from src.base.BaseViewControl import *
from src.viewcontrol.GameOverVC import GameOverVC

"""飞机大战游戏界面，所有界面都继承BaseViewControl"""


class PlaneGameVC(BaseViewControl):

    def __init__(self):
        super(PlaneGameVC, self).__init__()
        self.hero = Hero()
        self.enemy_group = pygame.sprite.Group()  # 敌机精灵组
        self.bg_music = pygame.mixer.Sound(BASE_RESOURCE_PATH_VOICE+"game_music.ogg")
        self.bg_music.set_volume(0.3)
        self.bg_music.play(-1)  # 循环播放

    def runTime(self):
        while True:
            if self.running:
                self._step()
                # 跟新显示
                self._onDraw()
            self._handEvent()
            # 更新画布
            pygame.display.flip()
            # 设置刷新帧率
            self.clock.tick(FRAME_PER_SEC)

    # 步骤帧
    def _step(self):
        super(PlaneGameVC, self)._step()
        self.hero.step(self.stepNum)
        #  按照时间间隔产生飞机（简单处理，策略可以修改）
        if not (self.stepNum % ENEMY_SMALL_CREATE_STEP):
            self.enemy_group.add(SmallEnemy())

        if not (self.stepNum % ENEMY_MIDDLE_CREATE_STEP):
            self.enemy_group.add(MiddleEnemy())

        if not (self.stepNum % ENEMY_BIG_CREATE_STEP):
            self.enemy_group.add(BigEnemy())

        for plane in self.enemy_group:
            plane.step(self.stepNum)

        self.__check_collide()

    def _onDraw(self):
        super(PlaneGameVC, self)._onDraw()
        self.hero.onDraw(self.screen)
        for plane in self.enemy_group:
            plane.onDraw(self.screen)

    def _handEvent(self):
        # 获取事件
        for event in pygame.event.get():
            # 判断事件是否为退出事件
            if event.type == pygame.QUIT:
                self.exitApp()
            elif event.type == pygame.USEREVENT:
                action = event.action
                if action == "gameover":
                    self.gameover()
                elif action == "play":
                    self.play()
                elif action == "exit":
                    self.exitApp()

        # 检测玩家键盘操作控制我方飞机方向
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_UP]:
            self.hero.moveUp()
        if key_pressed[pygame.K_DOWN]:
            self.hero.moveDown()
        if key_pressed[pygame.K_LEFT]:
            self.hero.moveLeft()
        if key_pressed[pygame.K_RIGHT]:
            self.hero.moveRight()

    # 碰撞检测
    def __check_collide(self):
        #  检测英雄发射的子弹与敌机的碰撞
        for bullet in self.hero.bullet_group:
            enemies_hit = pygame.sprite.spritecollide(bullet,
                                                      self.enemy_group, False, \
                                                      pygame.sprite.collide_mask)
            if enemies_hit:
                for e in enemies_hit:
                    e.hit(bullet.harm)
                bullet.kill()
        #  检测英雄飞机与敌机的碰撞
        enemies_hit2 = pygame.sprite.spritecollide(self.hero,
                                                   self.enemy_group, False, \
                                                   pygame.sprite.collide_mask)
        if enemies_hit2:
            for e2 in enemies_hit2:
                if e2.is_alive:
                    self.hero.hit(e2.harm)  # 受到伤害减少生命值
                    e2.hit(self.hero.harm)  # 受到伤害减少生命值

    # 开始游戏
    def play(self):
        self.hero = Hero()
        self.running = True
        self.bg_music.play(-1)  # 循环播放

    # 游戏结束英雄死了
    def gameover(self):
        print("====英雄死了===")
        self.running = False
        self.bg_music.stop()
        self.enemy_group.empty()
        gameover = GameOverVC()
        gameover.show()


